/**
 * 
 */
package captor.domainsystem;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import captor.lib.intl.MyIntl;
import captor.modelsystem.CaptorDomain;
import captor.modelsystem.Model;

import captor.crosscuttingdomainsystem.CrosscuttingDomain;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfo;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfoType.CompatibleDomainsType;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfoType.CompatibleDomainsType.DomainType;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfoType.CompatibleDomainsType.DomainType.PreDefinedPointcutsType.AbstractPointcutType;

/**
 * @author Carlos
 *
 */
public class BaseDomain extends CaptorDomain {

	/**
	 * 
	 */
	public BaseDomain() {
		// TODO Auto-generated constructor stub
	}

	public BaseDomain(Model model, String nameDomain, DomainInfo crosscuttingDomainInfo){
		super(model, nameDomain, crosscuttingDomainInfo);
		
		this.compatibleDomains = null;
	}
	
	
	/**
	 * @param model
	 * @param nameDomain
	 */
	public BaseDomain(Model model, String nameDomain) {
		super(model, nameDomain);
		
		// TODO Auto-generated constructor stub		
		this.compatibleDomains = null;
	}
	
	private List<CrosscuttingDomain> compatibleDomains;

	/**
	 * @return the compatibleDomains
	 */
	public List<CrosscuttingDomain> getCompatibleDomains() {
		return compatibleDomains;
	}

	/**
	 * @param compatibleDomains the compatibleDomains to set
	 */
	public void setCompatibleDomains(List<CrosscuttingDomain> compatibleDomains) {
		this.compatibleDomains = compatibleDomains;
	}
		
	public boolean loadCompatibleCrosscutingDomains(boolean flagXmlInfoFile){
				
		compatibleDomains = new LinkedList<CrosscuttingDomain>();
		
		if (crosscuttingDomainInfo==null){
			return false;
		}
		CompatibleDomainsType cb = crosscuttingDomainInfo.getCompatibleDomains();
		List compatibleDomainsList = cb.getDomain();
		
		if (compatibleDomainsList.isEmpty()){
			return false;
		}				
			
		if (flagXmlInfoFile)
			for(Iterator it1 = compatibleDomainsList.iterator(); it1.hasNext();) {
			    DomainType pt = (DomainType) it1.next();
			    CrosscuttingDomain crosscuttingDomain = new CrosscuttingDomain(this.model,pt.getDomainName());			    
			    List<AbstractPointcutType> preDefinedPointcuts = new LinkedList<AbstractPointcutType>();
			    if (pt.getPreDefinedPointcuts() != null){
			    	List preDefinedPointcutsList = pt.getPreDefinedPointcuts().getAbstractPointcut();
				    for(Iterator it2 = preDefinedPointcutsList.iterator(); it2.hasNext();){
				    	AbstractPointcutType abs = (AbstractPointcutType) it2.next();
				    	preDefinedPointcuts.add(abs);
				    }
			    }			    
			    crosscuttingDomain.setPreDefinedPointcuts(preDefinedPointcuts);
			    compatibleDomains.add(crosscuttingDomain);
			}
		else
			for(Iterator it1 = compatibleDomainsList.iterator(); it1.hasNext();) {
			    DomainType pt = (DomainType) it1.next();
			    CrosscuttingDomain crosscuttingDomain = new CrosscuttingDomain(this.model,pt.getDomainName(), null);			    
			    List<AbstractPointcutType> preDefinedPointcuts = new LinkedList<AbstractPointcutType>();
			    if (pt.getPreDefinedPointcuts() != null){
			    	List preDefinedPointcutsList = pt.getPreDefinedPointcuts().getAbstractPointcut();
				    for(Iterator it2 = preDefinedPointcutsList.iterator(); it2.hasNext();){
				    	AbstractPointcutType abs = (AbstractPointcutType) it2.next();
				    	preDefinedPointcuts.add(abs);
				    }
			    }			    
			    crosscuttingDomain.setPreDefinedPointcuts(preDefinedPointcuts);
			    compatibleDomains.add(crosscuttingDomain);
			}				
		
		return true;
	}
	
	public void validateDomain() throws DomainException, BaseDomainException{		
		super.validateDomain();		
		if (crosscuttingDomainInfo.isCrosscuttingDomain()){
			throw new BaseDomainException(MyIntl.MSG124);
		}
	}
	
	public class BaseDomainException extends DomainException{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1863412242533567771L;

		public BaseDomainException(){};
		
		public BaseDomainException(String msgException){
			super(msgException);
		}
		
		public String toString() {
			return MyIntl.MSG121;
			}
	}

	@Override
	public String getDomainDescription() {
		// TODO Auto-generated method stub
		return MyIntl.MSG123;
	}
}